/*------------------------------------------------------------------------------------------------------------------------------------------
--		SOURCE FILE:	Connect.cpp	  -	Represents the "Session Layer" of the ISO model.  Handles transmitting and receiving data at the 
--										serial port.
--
--		PROGRAM:		Wireless Modem Protocol Driver
--
--		FUNCTIONS:		DWORD WINAPI ReadPort (LPVOID stuff)
--						bool WritePort(PTTYINFO info, char charToWrite)
--						
--		DATE:			October 24, 2011
--
--		REVISIONS:		2.0
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER		Mike Zobac
--
--		NOTES:			All of these functions are passed a pointer to a TTYINFO struct.  This structure contains all of the variables which
--						would otherwise have to be global, or extern globals.
--------------------------------------------------------------------------------------------------------------------------------------------*/
#include "Connect.h"
#include "Translate.h"
#include "Control.h"
#include <mbstring.h>


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		ReadPort
--
--		DATE:			October 24, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		DWORD WINAPI ReadPort (HANDLE hPort)
--
--		RETURNS:		DWORD
--
--		NOTES:			This function sets up a loop which waits to recieve an EV_RXCHAR message, and upon receiving one reads a character
--						from the serial port, and passes it to the Presentation layer to be translated.
--------------------------------------------------------------------------------------------------------------------------------------------*/

DWORD WINAPI ReadPort (LPVOID stuff)
{
    char		charRead;
    DWORD       bRead, event, error, inQue;
    COMSTAT     comStat;
	PTTYINFO	pinfo = (PTTYINFO)stuff;
	OVERLAPPED		overLapped = {0};
	

	
    /* create a manual-reset event */
    overLapped.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);

    SetCommMask (pinfo->hPort, EV_RXCHAR);

    while (pinfo->bReading) 
    {
       
        if (WaitCommEvent (pinfo->hPort, &event, NULL))
        {
            ClearCommError (pinfo->hPort, &error, &comStat);
            if ((event & EV_RXCHAR) && comStat.cbInQue) 
            {
                inQue = comStat.cbInQue;
                while(inQue > 0)    //  while comm port que has data in it
                {
                    
                    if (!ReadFile(pinfo->hPort, &charRead, 1, &bRead, &overLapped))
                    {
                        ClearCommError (pinfo->hPort, &error, &comStat);
                    }
                    else 
                    {
                        if (bRead)

                        switch(charRead)
                        {
                            case '\x05':      // received ENQ for line
                                SetEvent(pinfo->ENQ);
								break;

                            case '\x06':     // received ACK
                                SetEvent(pinfo->ACK);
                                break;
                            
                            default:
                                buildFrame(charRead, pinfo);
                                break;
                        }
                    }
                    --inQue;
                }
            }
        } 
        else
        {
            ClearCommError (pinfo->hPort, &error, &comStat);
        }
    }
    PurgeComm(pinfo->hPort, PURGE_RXCLEAR);
    return 0L;
} 



/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		WritePort
--
--		DATE:			October 24, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		bool WritePort(PTTYINFO info, char charToWrite)
--
--		RETURNS:		bool
--
--		NOTES:			Writes the character passed to it by the Presentation Layer, or the WinProc to the serial port.
--------------------------------------------------------------------------------------------------------------------------------------------*/

bool WritePort(PTTYINFO pinfo, unsigned char * frame)
{
    DWORD			bWritten; 
    int				error;
    size_t			i, length;
	OVERLAPPED		overLapped = {0};
	
	overLapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);

	if(frame[0] == '\x02')
	{
		length = FRAMESIZE;
	}
	else
	{
		length = _mbslen(frame);
	}

    
	if (!WriteFile(pinfo->hPort, frame, length, &bWritten, &overLapped)) 
    {
        error = GetLastError();
            
		switch(error)
        {
            case 997:     // write operation isn't complete.
                
				break;
		}
    }
	WaitForSingleObject(overLapped.hEvent, 2000);
				
    return 0 ;
}

